package org.sport.dbImplement;

import org.sport.dbInterface.LinAppHisOperation;
import org.sport.model.entityModel.GrouptableEntity;
import org.sport.model.entityModel.LinkapplyhistoryEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.sport.repository.LinkapplyhistoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Allen on 2017/9/22.
 */
@Service
public class LinkApplyHisOpeImp implements LinAppHisOperation {


    @Autowired
    LinkapplyhistoryRepository linkapplyhistoryRepository;


    @Override
    public boolean addOneApplyInfo(LinkapplyhistoryEntity linkapply) {
        if(linkapply != null)
        {
            linkapplyhistoryRepository.saveAndFlush(linkapply);
            return true;
        }
        else
            return false;
    }

    /**
     * 更新User申请User的status
     *
     * @param ApplyUser
     * @param BeApplyUser
     * @return
     */
    @Override
    public boolean updateUserStatus(UsertableEntity ApplyUser, UsertableEntity BeApplyUser, String status) {

        linkapplyhistoryRepository.updateUserStatus(status,BeApplyUser,ApplyUser);
        LinkapplyhistoryEntity linkapplyhistoryEntity = new LinkapplyhistoryEntity();
        linkapplyhistoryEntity = linkapplyhistoryRepository.findBy2User(ApplyUser,BeApplyUser);
        if(linkapplyhistoryEntity.getStatus().equals(status))
            return true;
        else
            return false;

    }

    /**
     * 更新User申请Group状态，Group可以通过ownerId找寻到User进行状态修改
     *
     * @param ApplyUser
     * @param BeApplyGroup
     * @return
     */
    @Override
    public boolean updateGroupStatus(UsertableEntity ApplyUser, GrouptableEntity BeApplyGroup, String status) {
        linkapplyhistoryRepository.updateGroupStatus(status,BeApplyGroup,ApplyUser);
        LinkapplyhistoryEntity linkapplyhistoryEntity = new LinkapplyhistoryEntity();
        linkapplyhistoryEntity = linkapplyhistoryRepository.findByUserAndGroup(ApplyUser,BeApplyGroup);
        if(linkapplyhistoryEntity.getStatus().equals(status))
            return true;
        else
            return false;
    }

    @Override
    public boolean delOneCheatInfo(LinkapplyhistoryEntity linkapply) {

        linkapplyhistoryRepository.delete(linkapply);
        return true;
    }

    /**
     * 通过两个联系人确定唯一一条申请记录
     *
     * @param ApplyUser
     * @param BeApplyUser
     * @return
     */
    @Override
    public LinkapplyhistoryEntity findOneBy2User(UsertableEntity ApplyUser, UsertableEntity BeApplyUser) {
        return linkapplyhistoryRepository.findBy2User(ApplyUser,BeApplyUser);
    }

    /**
     * 通过一个User和一个Group确定唯一一条申请记录
     *
     * @param ApplyUser
     * @param BeApplyGroup
     * @return
     */
    @Override
    public LinkapplyhistoryEntity findOneByUserAndGroup(UsertableEntity ApplyUser, GrouptableEntity BeApplyGroup) {
        return linkapplyhistoryRepository.findByUserAndGroup(ApplyUser,BeApplyGroup);
    }

    @Override
    public List<LinkapplyhistoryEntity> findByApplyUserId(UsertableEntity ApplyUser) {


        return linkapplyhistoryRepository.findByApplyUserId(ApplyUser);
    }

    @Override
    public List<LinkapplyhistoryEntity> findByBeApplyUserId(UsertableEntity BeApplyUser) {

        return linkapplyhistoryRepository.findByBeApplyUserId(BeApplyUser);
    }

    @Override
    public List<LinkapplyhistoryEntity> findAllApplyAndBe() {

        return linkapplyhistoryRepository.findAll();
    }
}
